﻿
#include "common.h"
#include "sock.h"

#define BUFF_MAX (1024)
u8 recv_buff[BUFF_MAX];

int main(int argc, char **argv)
{
	signal(SIGPIPE, SIG_IGN);

	int sock_fd = get_sock("127.0.0.1",PORT);
	int conn_fd = get_connfd(sock_fd);

	fd_set readmask;
	fd_set fdset;
	FD_ZERO(&fdset);
	FD_SET(conn_fd, &fdset);

	while(1){
		readmask = fdset;
		int n = select(conn_fd+1,&readmask,NULL,NULL,NULL);
		if(n < 0){
			perror("select failed!");
			exit(EXIT_FAILURE);
		}
	    if(FD_ISSET(conn_fd, &readmask))
	    {
	        n = read(conn_fd, recv_buff, BUFF_MAX);
	        if(n < 0){
	            perror("read error!");
    			exit(EXIT_FAILURE);
	        } 

	        recv_buff[n] = '\0';	
	        printf("recv:%s", recv_buff);        
	        n = write(conn_fd,"server recv done.\n",strlen("server recv done.\n"));   
	        if(n < 0){
	            perror("write error!");
				exit(EXIT_FAILURE);
	        } 	        
        }
	}

	return 0;
}